package com.lixinkeji.widget

import android.R
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View.OnFocusChangeListener
import android.text.TextWatcher
import android.text.Editable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.widget.AppCompatEditText

/**
 * @ClassName CleanEditText
 * @Author lwc
 * @Email 1679114787@qq.com
 * @Date 2022/1/14 11:57
 */
class CleanEditText(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
    AppCompatEditText(context!!, attrs, defStyleAttr), OnFocusChangeListener, TextWatcher {
    //输入框四周的图标，数组里面放置的索引0-3对应，左，上，右，下
    private lateinit var mCompoundDrawables: Array<Drawable>

    //输入框右边的图标
    private var mCompoundRightDrawable: Drawable? = null

    //为输入框内容动态改变的监听，因为这个自定义view使用了TextWatcher，所以要在提供一个给用户使用
    private var mOnTextChangeListener: OnTextChangeListener? = null

    constructor(context: Context?) : this(context, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(
        context,
        attrs,
        R.attr.editTextStyle
    ) {

    }

    private fun init() {
        mCompoundDrawables = compoundDrawables
        mCompoundRightDrawable = mCompoundDrawables[2]
        setCleanIconVisible(false)
        onFocusChangeListener = this
    }

    //设置右边图标的显示与隐藏
    fun setCleanIconVisible(visible: Boolean) {
        //为空下面就没有意义，所以返回了
        if (mCompoundRightDrawable == null) return
        //不去给图标设置setBounds就可以设置图标的位置
        setCompoundDrawablesWithIntrinsicBounds(
            mCompoundDrawables[0],
            mCompoundDrawables[1],
            if (visible) mCompoundRightDrawable else null,
            mCompoundDrawables[3]
        )
    }

    //焦点改变事件，有焦点且内容为空 和 没有焦点都不显示图标
    override fun onFocusChange(v: View, hasFocus: Boolean) {
        if (hasFocus) {
            setCleanIconVisible(text!!.length > 0)
        } else {
            setCleanIconVisible(false)
        }
    }

    //右边图标的点击事件
    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            if (mCompoundRightDrawable != null) {
                //特别说明一下，getCompoundPaddingRight()=getPaddingRight()+mCompoundRightDrawable.getIntrinsicWidth()
                if (width - compoundPaddingRight < event.getX() && width - paddingRight > event.getX()) {
                    setText("")
                    setCleanIconVisible(false)
                    return true
                }
            }
        }
        return super.onTouchEvent(event)
    }

    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
        if (mOnTextChangeListener != null) mOnTextChangeListener!!.beforeTextChanged(
            s,
            start,
            count,
            after
        )
    }

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        setCleanIconVisible(s.toString().length > 0)
        if (mOnTextChangeListener != null) mOnTextChangeListener!!.onTextChanged(
            s,
            start,
            before,
            count
        )
    }

    override fun afterTextChanged(s: Editable) {
        if (mOnTextChangeListener != null) mOnTextChangeListener!!.afterTextChanged(s)
    }

    interface OnTextChangeListener {
        fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int)
        fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int)
        fun afterTextChanged(s: Editable?)
    }

    fun setOnTextChangeListener(`$OnTextChangeListener`: OnTextChangeListener?) {
        mOnTextChangeListener = `$OnTextChangeListener`
    }

    //动态设置右边图标
    fun setCompoundRightDrawable(drawable: Drawable?) {
        mCompoundRightDrawable = drawable
        setCompoundDrawablesWithIntrinsicBounds(
            mCompoundDrawables[0],
            mCompoundDrawables[1],
            if (text!!.isNotEmpty()) mCompoundRightDrawable else null,
            mCompoundDrawables[3]
        )
    }

    init {
        init()
    }
}